﻿using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Web.Configuration;
using msBsc.Cls;

namespace msBsc.AppLogic.DataAlc
{
    /// <summary>
    /// 地接客驾驶证
    /// </summary>
    /// Author:蒲崇彬
    /// Creation date:2015-8-31 13:08
    public class CBizDrivingLicense : CustBizObject
    {
        private CDmDjkDrivingLicense m_CDmDjkDrivingLicense = new CDmDjkDrivingLicense();
        private readonly List<CDmDjkDrivingLicense> _allAreaInfoList = null;
        private CDmDjkDrivingLicense oDmDjkDrivingLicense
        {
            get { return m_CDmDjkDrivingLicense; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizDrivingLicense()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkDrivingLicense.DJK_DRIVING_LICENSE);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["DRIVING_LICENSE_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {

            return true;
        }
        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }
        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }
        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }


        /// <summary>
        /// 添加驾驶证
        /// </summary>
        /// <param name="djackId">地接客Id</param>
        /// <param name="documentNumber">驾驶证号</param>
        /// <param name="documentPicture">驾驶证图片</param>
        /// <param name="name">姓名</param>
        /// <param name="gender">性别(0：女，1：男)</param>
        /// <param name="nationId">国籍Id</param>
        /// <param name="nationName">国籍名称</param>
        /// <param name="countryId">国家Id</param>
        /// <param name="countryName">国家名称</param>
        /// <param name="cityId">城市Id</param>
        /// <param name="cityName">城市名称</param>
        /// <param name="destinationId">目的地Id</param>
        /// <param name="destinationName">目的地名称</param>
        /// <param name="birthDate">生日</param>
        /// <param name="issueDate">初次领证日期</param>
        /// <param name="quasiType">准驾类型</param>
        /// <param name="effStartDate">有效起始时间</param>
        /// <param name="publisherId">添加人</param>
        /// <param name="ageLimit">有效年限</param>
        /// <returns></returns>
        ///  Author:任延华
        ///  Creation date:2015-9-6
        public string AddDrivingLicense(string djackId, string documentNumber, string documentPicture, string name, string gender, string nationId, string nationName, string countryId, string countryName, string cityId, string cityName, string destinationId, string destinationName, string birthDate, string issueDate, string quasiType, string effStartDate, string publisherId, string ageLimit)
        {
            #region 数据验证
            if (string.IsNullOrEmpty(djackId) || string.IsNullOrEmpty(documentNumber) || string.IsNullOrEmpty(documentPicture) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(gender) || string.IsNullOrEmpty(nationId) || string.IsNullOrEmpty(nationName) || string.IsNullOrEmpty(countryId) || string.IsNullOrEmpty(countryName) || string.IsNullOrEmpty(cityId) || string.IsNullOrEmpty(cityName) || string.IsNullOrEmpty(destinationId) || string.IsNullOrEmpty(destinationName) || string.IsNullOrEmpty(birthDate) || string.IsNullOrEmpty(issueDate) || string.IsNullOrEmpty(quasiType) || string.IsNullOrEmpty(effStartDate) || string.IsNullOrEmpty(ageLimit))
            {
                return "请完善驾照信息！";
            }

            //日期判断
            if (documentNumber.Length > 30)
            {
                return "驾驶证号最大长度不得超过30个字符";
            }
            if (name.Length > 20)
            {
                return "姓名最多不能超过20个字符";
            }
            if (!CommValiCollection.IsDate(birthDate))
            {
                return "出生日期格式不正确";
            }

            if (!CommValiCollection.IsDate(issueDate))
            {
                return "领证日期格式不正确";
            }

            if (!CommValiCollection.IsDate(effStartDate))
            {
                return "有效日期格式不正确";
            }

            if (!CommValiCollection.IsNumber(ageLimit))
            {
                return "有效年限只能是正整数";
            }
            int sex = 0;
            int.TryParse(gender, out sex);
            int age = 0;
            int.TryParse(ageLimit, out age);
            #endregion

            #region 添加车辆
            System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
            transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
            transactionOption.Timeout = new TimeSpan(0, 10, 0);
            using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
            {
                DataSet ds = new CDmDjkDrivingLicense();
                DataRow dr = ds.Tables[0].NewRow();
                dr["DRIVING_LICENSE_ID"] = this.DataObj.NewID();
                dr["DOCUMENT_NO"] = documentNumber;
                dr["REAL_NAME"] = name;
                dr["GENDER"] = sex;
                dr["NATIONLITY_ID"] = nationId;
                dr["NATIONLITY_NAME"] = nationName;
                dr["COUNTRY_ID"] = countryId;
                dr["COUNTRY_NAME"] = countryName;
                dr["CITY_ID"] = cityId;
                dr["CITY_NAME"] = cityName;
                dr["AREA_ID"] = destinationId;
                dr["AREA_NAME"] = destinationName;
                dr["BIRTH_DATE"] = birthDate;
                dr["ISSUE_DATE"] = issueDate;
                dr["QUASI_DRIVING_VEHICLE"] = quasiType;
                dr["EFFECTIVE_START_DATE"] = effStartDate;
                dr["USEFUL_LIFE"] = age;
                dr["CREATE_USERID"] = publisherId;
                dr["CREATE_DATE"] = this.dtCurrDateTime;
                dr["BELONGS_USERID"] = djackId;
                dr["IS_DELETE"] = 0;
                dr["DOCUMENT_IMAGE"] = documentPicture;
                ds.Tables[0].Rows.Add(dr);
                this.Update(ds);
                using (CBizCustomerBaseInfo baseInfo = new CBizCustomerBaseInfo())
                {
                    string[] certie = new string[] { "IS_DRIVING_LICENSE" };
                    string ret = baseInfo.UpdateCertificatesStatus(certie, djackId, true);
                    if (ret != "true")
                    {
                        return "更新用户标识出错";
                    }
                }
                ts.Complete();
                return "OK";
            }
            #endregion
        }

        /// <summary>
        /// 修改驾驶证
        /// </summary>
        /// <param name="licenseId">驾驶证id</param>
        /// <param name="documentNumber">驾驶证号</param>
        /// <param name="documentPicture">驾驶证图片</param>
        /// <param name="name">姓名</param>
        /// <param name="gender">性别(0：女，1：男)</param>
        /// <param name="nationId">国籍Id</param>
        /// <param name="nationName">国籍名称</param>
        /// <param name="countryId">国家Id</param>
        /// <param name="countryName">国家名称</param>
        /// <param name="cityId">城市Id</param>
        /// <param name="cityName">城市名称</param>
        /// <param name="destinationId">目的地Id</param>
        /// <param name="destinationName">目的地名称</param>
        /// <param name="birthDate">生日</param>
        /// <param name="issueDate">初次领证日期</param>
        /// <param name="quasiType">准驾类型</param>
        /// <param name="effStartDate">有效起始时间</param>
        /// <param name="ageLimit">有效年限</param>
        /// <returns></returns>
        ///  Author:蒲崇彬
        ///  Creation date:2015-9-14
        public string UpdateDriving(string licenseId, string documentNumber, string documentPicture, string name, string gender, string nationId, string nationName, string countryId, string countryName, string cityId, string cityName, string destinationId, string destinationName, string birthDate, string issueDate, string quasiType, string effStartDate, string ageLimit)
        {
            #region 数据验证
            if (string.IsNullOrEmpty(licenseId) || string.IsNullOrEmpty(documentNumber) || string.IsNullOrEmpty(documentPicture) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(gender) || string.IsNullOrEmpty(nationId) || string.IsNullOrEmpty(nationName) || string.IsNullOrEmpty(countryId) || string.IsNullOrEmpty(countryName) || string.IsNullOrEmpty(cityId) || string.IsNullOrEmpty(cityName) || string.IsNullOrEmpty(destinationId) || string.IsNullOrEmpty(destinationName) || string.IsNullOrEmpty(birthDate) || string.IsNullOrEmpty(issueDate) || string.IsNullOrEmpty(quasiType) || string.IsNullOrEmpty(effStartDate) || string.IsNullOrEmpty(ageLimit))
            {
                return "请完善驾照信息！";
            }

            //日期判断
            if (documentNumber.Length > 30)
            {
                return "驾驶证号最大长度不得超过30个字符";
            }
            if (name.Length > 20)
            {
                return "姓名最多不能超过20个字符";
            }
            if (!CommValiCollection.IsDate(birthDate))
            {
                return "出生日期格式不正确";
            }

            if (!CommValiCollection.IsDate(issueDate))
            {
                return "领证日期格式不正确";
            }

            if (!CommValiCollection.IsDate(effStartDate))
            {
                return "有效日期格式不正确";
            }

            if (!CommValiCollection.IsNumber(ageLimit))
            {
                return "有效年限只能是正整数";
            }
            int sex = 0;
            int.TryParse(gender, out sex);
            int age = 0;
            int.TryParse(ageLimit, out age);
            #endregion

            DataSet ds=this.GetDrivingLicenseByID(licenseId);
            if (ds==null||ds.Tables[0].Rows.Count<1)
            {
                return "证件信息不存在";
            }
            DataRow dr = ds.Tables[0].Rows[0];
            dr["DOCUMENT_NO"] = documentNumber;
            dr["REAL_NAME"] = name;
            dr["GENDER"] = sex;
            dr["NATIONLITY_ID"] = nationId;
            dr["NATIONLITY_NAME"] = nationName;
            dr["COUNTRY_ID"] = countryId;
            dr["COUNTRY_NAME"] = countryName;
            dr["CITY_ID"] = cityId;
            dr["CITY_NAME"] = cityName;
            dr["AREA_ID"] = destinationId;
            dr["AREA_NAME"] = destinationName;
            dr["BIRTH_DATE"] = birthDate;
            dr["ISSUE_DATE"] = issueDate;
            dr["QUASI_DRIVING_VEHICLE"] = quasiType;
            dr["EFFECTIVE_START_DATE"] = effStartDate;
            dr["USEFUL_LIFE"] = age;
            dr["DOCUMENT_IMAGE"] = documentPicture;
            ds.Tables[0].TableName = this.TableName;
            this.Update(ds);

            return "OK";
        }

        /// <summary>
        /// 跟据 驾驶证 id 获取信息
        /// </summary>
        /// <param name="licenseId">驾驶证 id </param>
        /// <returns>DataSet</returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-14 09：28
        public DataSet GetDrivingLicenseByID(string licenseId)
        {

            if (string.IsNullOrEmpty(licenseId))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("licenseId", ParameterDirection.Input, EDbType.String, 36, licenseId));

            return this.DataObj.QueryBySQL(@"SELECT [DRIVING_LICENSE_ID],[DOCUMENT_NO],[REAL_NAME],[GENDER],[NATIONLITY_ID],[NATIONLITY_NAME],
[COUNTRY_ID],[COUNTRY_NAME],[CITY_ID],[CITY_NAME],[AREA_ID],[AREA_NAME],[BIRTH_DATE],[ISSUE_DATE],[QUASI_DRIVING_VEHICLE],[EFFECTIVE_START_DATE],
[USEFUL_LIFE],[IS_DELETE],[DOCUMENT_IMAGE] FROM [dbo].[DJK_DRIVING_LICENSE](NOLOCK)  WHERE [DRIVING_LICENSE_ID]=@licenseId", parmCollection);
        }

        /// <summary>
        /// 跟据 地接客 id 获取信息
        /// </summary>
        /// <param name="licenseId">地接客 id </param>
        /// <returns>DataSet</returns>
        /// Author:任延华
        /// Creation date:2015-9-24
        public DataSet GetDrivingLicenseDjackID(string djackId)
        {

            if (string.IsNullOrEmpty(djackId))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("djackId", ParameterDirection.Input, EDbType.String, 36, djackId));

            return this.DataObj.QueryBySQL(@"SELECT [DRIVING_LICENSE_ID],[DOCUMENT_NO],[REAL_NAME],[GENDER],[NATIONLITY_ID],[NATIONLITY_NAME],
[COUNTRY_ID],[COUNTRY_NAME],[CITY_ID],[CITY_NAME],[AREA_ID],[AREA_NAME],[BIRTH_DATE],[ISSUE_DATE],[QUASI_DRIVING_VEHICLE],[EFFECTIVE_START_DATE],
[USEFUL_LIFE],[IS_DELETE],[DOCUMENT_IMAGE] FROM [dbo].[DJK_DRIVING_LICENSE](NOLOCK)  WHERE [IS_DELETE]=0 and [BELONGS_USERID]=@djackId", parmCollection);
        }
    }
}
